﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WoWConnecter.Entities;

namespace WoWConnecter
{
    public partial class RealmClient
    {
        [PacketMethod(RealmServerOpCode.SMSG_CHAR_ENUM)]
        private void HandleCharacterList(RealmPacketIn packet)
        {
            byte count = packet.ReadByte();
            CharacterCount = 0;
            Characters = new UserCharacter[10];

            for (int i = 0; i < count; i++)
            {
                Characters[i] = new UserCharacter(packet);
                CharacterCount++;
            }
            if (CharacterSelection != null && firstCharEnum)
            {
                firstCharEnum = false;
                CharacterSelection(Characters);
            }
        }

        [PacketMethod(RealmServerOpCode.SMSG_CHARACTER_LOGIN_FAILED)]
        private void HandleCharacterLoginFailed(RealmPacketIn packet)
        {
            Console.WriteLine();
        }

        [PacketMethod(RealmServerOpCode.SMSG_LOGIN_VERIFY_WORLD)]
        private void HandleCharacterWorldVerification(RealmPacketIn packet)
        {
            if (EnteredWorld != null)
                EnteredWorld(ActiveCharacter);
        }

        [PacketMethod(RealmServerOpCode.SMSG_CHAR_CREATE)]
        private void HandleCharacterCreation(RealmPacketIn packet)
        {
            LoginErrorCode result = (LoginErrorCode)packet.ReadByte();
            if (result == LoginErrorCode.CHAR_CREATE_SUCCESS && creatingCharacter != null)
            {
                Characters[CharacterCount++] = creatingCharacter;
            }

            creatingCharacter = null;
            if (CharacterCreationResult != null)
                CharacterCreationResult(result, Characters[CharacterCount - 1]);
        }

        public void SendCharacterCreation(Character character)
        {
            RealmPacketOut packet = new RealmPacketOut(RealmServerOpCode.CMSG_CHAR_CREATE);
            packet.WriteCString(character.Name);
            packet.WriteByte((byte)character.Race);
            packet.WriteByte((byte)character.Class);
            packet.WriteByte((byte)character.Gender);
            packet.WriteByte(character.Skin);
            packet.WriteByte(character.Face);
            packet.WriteByte(character.HairStyle);
            packet.WriteByte(character.HairColour);
            packet.WriteByte(character.FacialHair);
            packet.WriteByte(character.Outfit);
            Send(packet);
        }

        public void SendCharacterLogin(UserCharacter character)
        {
            RealmPacketOut packet = new RealmPacketOut(RealmServerOpCode.CMSG_PLAYER_LOGIN);
            packet.Write(character.Id);
            Send(packet);
        }

    }
}
